package com.hanbang.taitaigou.ui.widget.banner;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Administrator on 2016/1/20.
 */
public class CircleDotsView extends View {

    private int circleColor = Color.RED;
    private int radius = 10;
    private Paint paint = new Paint();

    public CircleDotsView(Context context) {
        this(context, null);
    }

    public CircleDotsView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleDotsView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint.setAntiAlias(true);
        radius = dip2px(getContext(), 5);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        paint.setColor(circleColor);
        canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
    }

    public int getCircleColor() {
        return circleColor;
    }

    public void setCircleColor(int circleColor) {
        this.circleColor = circleColor;
        invalidate();
    }

    public int getRadius() {
        return radius;
    }

    /**
     * 设置半径
     */
    public void setRadius(int radius) {
        this.radius = radius;
        invalidate();
    }


    /**
     * 将dip或dp值转换为px值，保证尺寸大小不变
     *
     * @param dipValue
     * @return
     */
    private int dip2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dipValue * scale + 0.5f);
    }
}
